package com.tqw.chd.controller;

import com.tqw.chd.service.FileService;
import com.tqw.chd.vo.ImageVO;
import com.tqw.common.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Alex
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 图片上传
     * 请求路径:/file/upload
     * 请求类型:post
     * 请求参数:file
     * 返回值:SysResult(imageVO)
     * @return
     */
    @PostMapping("/upload")
    public JsonResult<ImageVO> upload(MultipartFile file){

        ImageVO imageVO = fileService.upload(file);
        if (imageVO==null){
            return JsonResult.build().code(JsonResult.UNKNOWN_ERROR);
        }
        return JsonResult.build().code(JsonResult.SUCCESS);
    }

    /**
     * 删除图片
     * 请求路径:/file/deleteFile
     * 请求类型:delete
     * 请求参数:virtuaPath
     * 返回值:SysResult
     * @return
     */
    public JsonResult<?> deleteFile(String virtualPath){

        fileService.deleteFile(virtualPath);
        return JsonResult.build().code(JsonResult.SUCCESS);

    }

}
